/*
* $Id: QBInfoPointOperation.cpp 110 2010-07-01 15:22:02Z volok-aleksej@yandex.ru $
*
* Copyright 2010 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "QBInfoPointOperation.h"
#include "../SqlBinder.h"
#include "../Tables/TableNames.h"

CQBInfoPointOperation::CQBInfoPointOperation(){}
CQBInfoPointOperation::~CQBInfoPointOperation(){}

CQBInfoPointOperation::content::content(){
	Clear();
}
void CQBInfoPointOperation::content::Clear(){
	s_nIdPoint = 0;
	s_nIdFamily = 0;
	s_strNameFamily.clear();
	s_nCount = 0;
	s_bDead = false;
}
void CQBInfoPointOperation::content::Set(const std::vector<CTableItem>& aContent){
	for(std::vector<CTableItem>::const_iterator it = aContent.begin(); it != aContent.end(); it++){
		if(it->m_strNameColumn == _T("name"))
			s_strNameFamily = it->m_strValueColumn;
		else if(it->m_strNameColumn == _T("is_dead"))
			s_bDead = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("count"))
			s_nCount = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("id_family"))
			s_nIdFamily = _ttoi(it->m_strValueColumn.c_str());
	}
}

IResult* CQBInfoPointOperation::GetValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("SELECT "));
	strRequest = strRequest + FAMILIES_TABLE_NAME + _T(".name, id_family, is_dead, count(id_queen_bee) as 'count' from ");
	strRequest = strRequest + FAMILIES_TABLE_NAME + _T(" left join ") + QUEEN_BEE_TABLE_NAME;
	binder.SetRequest(strRequest + _T(" using(id_family) where id_point=? group by id_family, is_dead;"));
	binder.SetValue(0, m_content.s_nIdPoint);
	return FillContent(pDatabase->ExecRequest(binder.GetRequest()));
}

IOperation::i_content* CQBInfoPointOperation::CreateContent(){
	return new content;
}
